<template>
    <div class="chat-container">
      <div class="chat-header">
        <h2>AI 助手</h2>
      </div>
      
      <div class="chat-messages" ref="messageContainer">
        <div v-for="(message, index) in messages" 
             :key="index" 
             :class="['message', message.type]">
          <div class="avatar">
            <img :src="message.type === 'user' ? userAvatar : aiAvatar" />
          </div>
          <div class="message-content">
            {{ message.content }}
          </div>
        </div>
      </div>
  
      <div class="chat-input">
        <input 
          v-model="inputMessage" 
          @keyup.enter="sendMessage"
          placeholder="请输入您的问题..."
        />
        <button @click="sendMessage">发送</button>
      </div>
    </div>
  </template>
  
  <script>
  export default {
    name: 'AIChatWindow',
    data() {
      return {
        messages: [],
        inputMessage: '',
        userAvatar: 'https://cdn.vuetifyjs.com/images/john.jpg',
        aiAvatar: 'https://cdn.vuetifyjs.com/images/logos/v.png'
      }
    },
    methods: {
      async sendMessage() {
        if (!this.inputMessage.trim()) return
        
        // 添加用户消息
        this.messages.push({
          content: this.inputMessage,
          type: 'user'
        })
  
        const userMessage = this.inputMessage
        this.inputMessage = ''
  
        try {
          const response = await this.$axios.post('/api/chat', {
            message: userMessage
          })
  
          // 添加AI回复
          this.messages.push({
            content: response.data.reply,
            type: 'ai'
          })
        } catch (error) {
          console.error('发送消息失败:', error)
        }
  
        this.$nextTick(() => {
          this.scrollToBottom()
        })
      },
      scrollToBottom() {
        const container = this.$refs.messageContainer
        container.scrollTop = container.scrollHeight
      }
    }
  }
  </script>
  
  <style scoped>
  .chat-container {
    width: 800px;
    height: 600px;
    margin: 0 auto;
    border-radius: 10px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
    display: flex;
    flex-direction: column;
  }
  
  .chat-header {
    padding: 20px;
    background: #7957d5;
    color: white;
    border-radius: 10px 10px 0 0;
  }
  
  .chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    background: #f8f9fa;
  }
  
  .message {
    display: flex;
    margin-bottom: 20px;
    align-items: flex-start;
  }
  
  .message.user {
    flex-direction: row-reverse;
  }
  
  .avatar {
    width: 40px;
    height: 40px;
    margin: 0 10px;
  }
  
  .avatar img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
  }
  
  .message-content {
    max-width: 70%;
    padding: 12px 16px;
    border-radius: 12px;
    background: white;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
  }
  
  .user .message-content {
    background: #7957d5;
    color: white;
  }
  
  .chat-input {
    padding: 20px;
    background: white;
    border-top: 1px solid #eee;
    display: flex;
  }
  
  .chat-input input {
    flex: 1;
    padding: 10px;
    border: 1px solid #ddd;
    border-radius: 4px;
    margin-right: 10px;
  }
  
  .chat-input button {
    padding: 10px 20px;
    background: #7957d5;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
  }
  
  .chat-input button:hover {
    background: #6544c1;
  }
  </style>